/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function loadStory(page, style, flag) {
	if (page == null) {
		page = 1;
	}
	f$("curPage").value = page;
	var ajax = new Ajax();
	ajax.requestURL = "story.action";
	ajax.setVar("flag", f$("flag").value);
	ajax.setVar("page", page);
	ajax.setVar("pageCount", (f$("pageCount").value == "" ? 20
			: f$("pageCount").value));
	ajax.method = "GET";
	ajax.onLoading = function() {
		f$("storycontent").innerHTML = "加载数据中...";
	}
	ajax.onCompletion = function() {
		var jsonObj = JSON.parse(ajax.xmlhttp.responseText);
		renderContent(jsonObj, style, flag);
		renderSeg(jsonObj, style, flag);
	}
	ajax.runAJAX();
}
function renderContent(obj, style, flag) {
	f$("storycontent").innerHTML = "";
	var msgLen = obj.length;
	if (msgLen == 1) {
		f$("storycontent").parentNode.style.border = "0";
	}

	for ( var i = 1; i < msgLen; i++) {
		var storyHTML = "<div style='border:1px solid " + style
				+ ";padding:10px;margin:5px;color:black'>";
		storyHTML += "<div><span style='float:left'>作者:&nbsp;<font style='color:"
				+ style
				+ ";font-size:13px;font-weight:bold'>"
				+ obj[i].author
				+ "</font></span><span style='float:right'>"
				+ "创建时间:&nbsp;"
				+ obj[i].gmtCreate + "</span></div><br><br>";
		storyHTML += "<div style='font-size:16px;font-weight:bold;float:left'  ><a style='color: "
				+ style
				+ ";' href='story_detail.action?id="
				+ obj[i].id
				+ "&flag=" + flag + "'>" + obj[i].storyName + "</a></div>";
		storyHTML += "<br>";
		storyHTML += "<div id='" + obj[i].id
				+ "' style='word-break : break-all;word-wrap:break-word;'>"
				+ blankRep(2) + obj[i].storyContent + "</div>";
		storyHTML += "<br> ";
		storyHTML += blankRep(1) + "浏览(" + obj[i].browseAmount + ")次"
				+ blankRep(5) + "评论(" + obj[i].commentAmount + ")条"
				+ blankRep(10) + "<span style='float:right'>修改时间:&nbsp;"
				+ obj[i].gmtModify;
		storyHTML += "<span></div>";
		f$("storycontent").innerHTML += storyHTML;
	}
}
function clickFile(id) {
	f$(id).click();
}

function renderSeg(obj, style, flag) {
	var curPage = f$("curPage").value;
	var totalCount = obj[0].totalCount;
	var pageCount = obj[0].pageCount;
	var seg = Math.ceil(totalCount / pageCount);
	f$("page_seg").innerHTML = "<div>";
	for ( var i = 1; i <= seg; i++) {
		var _out = "this.style.backgroundColor='white';this.style.color='navy';";
		var _over = "this.style.backgroundColor='" + style
				+ "';this.style.color='white';";
		if (curPage == i) {
			_out = _over;
			_over = "";
		}
		f$("page_seg").innerHTML += "<div style='width:10px;height:10px;float:left;' >&nbsp;</div> "
				+ "<div onmouseout=\""
				+ _out
				+ "\""
				+ " onmouseover=\""
				+ _over
				+ "this.style.cursor='pointer'\"   onclick=\"loadStory('"
				+ i
				+ "','"
				+ style
				+ "','"
				+ flag
				+ "')\""
				+ " style='"
				+ ((curPage == i) ? "color:white;background-color:" + style
						+ ";" : "")
				+ "line-height:20px;text-align:center;float:left;width:20px;height:20px;border:1px solid "
				+ style + ";'>" + (i) + "</div>";
	}
	f$("page_seg").innerHTML += "</div>";
}

// ***********************上传图片开始.....

 
var domainBase;

function uploadfile(btn, db) {

	if (domainBase == null) {
		domainBase = db;
	}
	if (!checkFile()) {
		return;
	}
	var fn = f$("picpath").value.substring(f$("picpath").value
			.lastIndexOf("\\") + 1);
	f$("picname").value = fn;
	if (!checkFileAviable(fn)) {
		return;
	}
	btn.disabled = true;
	f$("bar").style.display = "block";
	f$("processBar").style.width = 0;
	requestFileStatus(fn);
	f$("uploadForm").submit();
}
function checkFile() {
	if (f$("picpath").value == "") {
		alert("文件不能为空");
		return false;
	}
	return true;
}

function checkFileAviable(fn) {
	var ajax = new Ajax();
	var res = true;
	ajax.requestURL = "upload_ajax_check.action";
	ajax.method = "GET";
	ajax.setVar("flag", "01");
	ajax.setVar("picname", fn);
	ajax.onCompletion = function() {
		var resText = ajax.xmlhttp.responseText;
		if (resText == "0") {
			alert(fn + "]非法的文件格式");
			res = false;
		} else if (resText == '-1') {
			alert(fn + " 已经存在该文件了");
			res = false;
		}else if (resText == "1") {
			res = true;
		}
	}
	ajax.runAJAX(null, false);
	return res;
}

function requestFileStatus(fn) {
	var ajax = new Ajax();
	ajax.requestURL = "upload_ajax.action";
	ajax.method = "GET";
	ajax.onCompletion = function() {
		var resText = ajax.xmlhttp.responseText;
		if (resText != null && resText != "") {
			if (resText.indexOf("!") == 0) {
				finishProcessBar(true, fn,resText.substring(1));
			} else if (resText.indexOf("_") == 0) {
				window.setTimeout(function() {
					  requestFileStatus(fn);
					}, 100);
				renderProcessBar(resText.substring(1));					
			} else {
				alert("上传出错!");
				finishProcessBar(false);
			}
		}
	};
	ajax.runAJAX();
}
function delFile() {

}

function renderProcessBar(resText) {
	var arr = resText.split(",");
	f$("processBar").style.width = arr[4] + "%";
	f$("processDesc").innerHTML = "上传[" + arr[0] + "/" + arr[1] + "] 当前速度["
			+ arr[2] + "],剩下时间[" + arr[3] + "]";
}

function finishProcessBar(suc, fn,resText) {
	var arr = resText.split(",");
	if (suc) {
		f$('picinfo').innerHTML += "<div>图片名称:" + fn + ",链接:" + domainBase
				+ "?id=" + arr[7] + "</div>";
		f$("processBar").style.width = 100 + "%";
		window.setTimeout(function() {
			f$("bar").style.display = "none";
		}, 1000);
	}
	// f$("processBar").style.display="none";
	f$("uploadBtn").disabled = false;
 
}

function storycommit(obj) {
	if (f$("storyname").value == "") {
		alert("标题不能为空!");
		return;
	}

	if (oEditer.GetXHTML(true) == "") {
		alert("内容不能为空!");
		return;
	}
	obj.disabled = true;
	document.forms[0].action = "story_add.action";
	var t = document.forms[0];
	t.setAttribute("target", "");
	t.setAttribute("enctype", "");
	document.forms[0].submit();
}